package com.dex4d.ui.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dex4d.BuildConfig;
import com.dex4d.R;
import com.dex4d.constants.AppConstants;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.model.User;
import com.dex4d.ui.demo.HomeActivity;
import com.dex4d.ui.main.model.TradeResult;
import com.dex4d.ui.mine.activity.AuthorityReferralActivity;
import com.dex4d.ui.mine.activity.AuthoritySwapActivity;
import com.dex4d.utils.BaseUtils;
import com.dex4d.utils.Commons;

import butterknife.BindView;

/**
 * Created by chengyuchun on 2018/11/22.
 */
public class TradeDoneActivity extends AppToolBarBaseActivity implements Commons.BUNDLE_KEYS {

    @BindView(R.id.tv_trade_type)
    TextView tv_trade_type;

    @BindView(R.id.tv_trade_amount)
    TextView tv_trade_amount;

    @BindView(R.id.tv_from_label)
    TextView tv_from_label;

    @BindView(R.id.tv_from_address)
    TextView tv_from_address;

    @BindView(R.id.tv_to_label)
    TextView tv_to_label;

    @BindView(R.id.tv_to_address)
    TextView tv_to_address;

    @BindView(R.id.tv_gas_consumption)
    TextView tv_gas_consumption;

    @BindView(R.id.tv_hash)
    TextView tv_hash;

    private int mType;
    private String mD4DAmount;

    @Override
    protected void initToolbar() {
        hideBackView();
        showRightDone();
        getToolbarRight().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mType == TradeResult.FLAG_BUY || mType == TradeResult.FLAG_REINVEST
                        || mType == TradeResult.FLAG_SELL || mType == TradeResult.FLAG_SWAP){
                    openActivity(HomeActivity.class);
                }else if(mType == TradeResult.FLAG_BUY_REFERRAL){
                    Intent intent = new Intent(TradeDoneActivity.this,AuthorityReferralActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }else if(mType == TradeResult.FLAG_BUY_SWAP){
                    Intent intent = new Intent(TradeDoneActivity.this,AuthoritySwapActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }else {
                    HomeActivity.goBackToMain(TradeDoneActivity.this,AppConstants.TAB_WALLET);
                }

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_trade_done;
    }

    @Override
    protected void initVariables() {
        final Bundle bundle = getIntent().getExtras();
        mType = bundle.getInt(TRADE_TYPE);
        String poolSymbol = bundle.getString(POOL_SYMBOL);
        mD4DAmount = bundle.getString(D4D_AMOUNT);
        String coinAmount = bundle.getString(COIN_AMOUNT);
        final String gas = bundle.getString(GAS_CONSUMPTION); //待修改
        if(mType == TradeResult.FLAG_BUY || mType == TradeResult.FLAG_REINVEST){
            //购买、再投资、购买权限、转账
            tv_trade_type.setText(getString(R.string.d4d_confirm_send_amount));
            //购买的coinAmount从前一个页面传过来的值是wei单位的，这里需要转下单位，其他交易类型不需要转
            tv_trade_amount.setText(coinAmount
                    + " " + poolSymbol.toUpperCase());
            tv_from_address.setText(BaseUtils.formatTxHash(User.getInstance().getAddress()));
            tv_to_address.setText(BaseUtils.formatTxHash(BuildConfig.CONTRACT_DEX4D_PROTOCOL));
        }else if(mType == TradeResult.FLAG_SELL || mType == TradeResult.FLAG_SWAP){
            tv_trade_type.setText(getString(R.string.d4d_confirm_sell_amount));
            tv_trade_amount.setText(mD4DAmount + " D4D");
            tv_from_address.setText(BaseUtils.formatTxHash(User.getInstance().getAddress()));
            tv_to_address.setText(BaseUtils.formatTxHash(BuildConfig.CONTRACT_DEX4D_PROTOCOL));
        }else if(mType == TradeResult.FLAG_BUY_REFERRAL || mType == TradeResult.FLAG_BUY_SWAP){
            tv_trade_type.setText(getString(R.string.d4d_confirm_send_amount));
            tv_trade_amount.setText(coinAmount + " ETH");
            tv_from_address.setText(BaseUtils.formatTxHash(User.getInstance().getAddress()));
            tv_to_address.setText(BaseUtils.formatTxHash(BuildConfig.CONTRACT_DEX4D_PROTOCOL));
        }else if(mType == TradeResult.FLAG_WITHDRAW){
            tv_trade_type.setText(getString(R.string.d4d_confirm_send_amount));
            tv_trade_amount.setText(coinAmount + " "+getIntent().getStringExtra(WITHDRAW_SYMBOL));
            tv_from_address.setText(BaseUtils.formatTxHash(User.getInstance().getAddress()));
            tv_to_address.setText(BaseUtils.formatTxHash(BuildConfig.CONTRACT_DEX4D_PROTOCOL));
        }else if(mType == TradeResult.FLAG_TRANSFER){
            //转账
            tv_trade_type.setText(getString(R.string.d4d_confirm_send_amount));
            tv_from_label.setText(getString(R.string.d4d_confirm_send_from));
            tv_to_label.setText(getString(R.string.d4d_confirm_send_to));
            tv_trade_amount.setText(coinAmount + " "+getIntent().getStringExtra(TO_SYMBOL).toUpperCase());
            tv_from_address.setText(BaseUtils.formatTxHash(User.getInstance().getAddress()));
            tv_to_address.setText(BaseUtils.formatTxHash(getIntent().getStringExtra(TO_ADDRESS)));
        }

        tv_gas_consumption.setText(gas + " ETH");
        tv_hash.setText(bundle.getString(TRANS_HASH));
    }

    @Override
    protected void loadData() {

    }
}
